#include <iostream>
#include <string>
#include <vector>

using namespace std;

std::vector<std::string> tokenize(const std::string &self,
                                  const std::string &chars = " ");

int main() {
    vector<string> msg{"Hello", "C++",     "World",
                       "from",  "VS Code", "and the C++ extension!"};
    vector<string> msg{"Hello", "C++",     "World",
                       "from",  "VS Code", "and the C++ extension!"};

    string helloWorld("Hello C++ World from VS Codeand the C++ extension!");

    vector<string> msgs = tokenize(helloWorld, "the");

    cout << "msgs.size(length) = " << msgs.size() << endl;

    for (const string &word : msgs) {
        cout << word << " ";
    }
    cout << endl;
}

std::vector<std::string> tokenize(const std::string &self,
                                  const std::string &chars) {
    std::vector<std::string> tokens(1);
    std::string map(256, '\0');
    for (const unsigned char &ch : chars)
        map[ch] = '\1';
    for (const unsigned char &ch : self) {
        /**/ if (!map.at(ch))
            tokens.back().push_back(char(ch));
        else if (tokens.back().size())
            tokens.push_back(std::string());
    }
    while (tokens.size() && tokens.back().empty())
        tokens.pop_back();
    return tokens;
}

size_t split(std::vector<std::string> &tokens, const std::string &self,
             const std::string &delimiters) {
    std::string str;
    tokens.clear();
    for (auto &ch : self) {
        if (delimiters.find_first_of(ch) != std::string::npos) {
            if (str.size())
                tokens.push_back(str), str = "";
            tokens.push_back(std::string() + ch);
        } else
            str += ch;
    }
    return str.empty() ? tokens.size() : (tokens.push_back(str), tokens.size());
}